package com.weirong.smartvault.backservice.service;

import com.weirong.smartvault.dao.CashboxConfRepository;
import com.weirong.smartvault.entity.CashboxConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

/**
 * 于2017/12/7由 KFEB4 创建 .
 */
@Service
public class CashboxConfServiceImpl implements CashboxConfService{
    @Autowired
    CashboxConfRepository cashboxConfRepository;
    @Override
    public Page<CashboxConf> findAllByUnitNoWithPaging(Integer unitNo, Integer page, Integer count) {
        Specification<CashboxConf> specification = (root, criteriaQuery, criteriaBuilder) -> {
            Path<Integer> No = root.get("unitNo");
            Predicate predicate = criteriaBuilder.equal(No,unitNo);
            return criteriaBuilder.and(predicate);
        };

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, count, sort);
        return cashboxConfRepository.findAll(specification,pageable);
    }

    @Override
    public Long countByUnitNo(Integer unitNo) {
        Specification<CashboxConf> specification = (root, criteriaQuery, criteriaBuilder) -> {
            Path<Integer> No = root.get("unitNo");
            Predicate predicate = criteriaBuilder.equal(No,unitNo);
            return criteriaBuilder.and(predicate);
        };
        return cashboxConfRepository.count(specification);
    }
}
